/*
\ * EatsStage.cpp
 *
 *  Created on: 04/02/2013
 *      Author: eusyar
 */

#include "game/entities/EatsStage.h"
#include "game/towers/Catodix.h"
#include "util/Helpers.h"

#include <string>

using namespace eats_helpers;
using namespace std;

EatsStage::EatsStage(std::string _mapLocation, int nemliniumPosX , int nemliniumPosY ) {
	mapLocation = _mapLocation;
	isTowerSelected = false;
	nemliniumOnStock = 0;
	_nemliniumPosition.x = nemliniumPosX;
	_nemliniumPosition.y = nemliniumPosY;
	_totalTime = 80000;
}

EatsStage::~EatsStage() {
	// TODO Auto-generated destructor stub
}

bool EatsStage::setUp()
{

	map = new Map  ( _working_surface , mapLocation, true);
	hud = new Mural( _working_surface , CoordinatesHelper::bottonLeftWidth(), CoordinatesHelper::bottonLeftHeight(100));

	map -> _canonical_name = "MAPA 01";

	//_waveHolder->setUp();

	objList.push_back(map);
	objList.push_back(hud);

	{
		//Essas referencias eu vou perder msm.... triste... triste
		Point * tempPointer = new Point;
		tempPointer->x =_nemliniumPosition.x;
		tempPointer->y =_nemliniumPosition.y;

		Nemlinium * nemlinium = new Nemlinium(_working_surface, tempPointer->x, tempPointer->y);
		objList.push_back(nemlinium);
		nemliniumList.push_back(nemlinium);
		for(int i=1; i < nemliniumOnStock; i++){
			tempPointer = new Point;
			tempPointer->x = (_nemliniumPosition.x  );
			tempPointer->y = (_nemliniumPosition.y + ResourceHelper::getNemliniumTileSize() );
			nemlinium = new Nemlinium(_working_surface, tempPointer->x, tempPointer->y);
			_nemliniumPosition.x +=  ResourceHelper::getNemliniumTileSize();
			if(! i % 3){
				_nemliniumPosition.x -=  3*ResourceHelper::getNemliniumTileSize() - 10;
				_nemliniumPosition.y +=  ResourceHelper::getNemliniumTileSize();
			}
			objList.push_back(nemlinium);
			nemliniumList.push_back(nemlinium);
		}
	}

	_initTime = SDL_GetTicks();

	cout << "\n|ERROR|"<<SDL_GetError() <<"\n"<< endl;

	return GameState::setUp();

}

bool EatsStage::render()
{
	bool ret = true;
	ret = ret && GameState::render();
	_waveHolder->render();
	return ret;
}

void EatsStage::update()
{

	if(nemliniumList.size() == 0){
		cout << "|INFO| ENVIANDO MSG DE TERMINO DO JOGO" << endl;
		GameConteiner::getInstance()->getGameManager()->turnOFF();
	}

	if( _totalTime + _initTime < SDL_GetTicks() ){
		cout << "|INFO| Fase concluida" << endl;
		state = finished;
	}

	_waveHolder->update();

	GameState::update();


}

void EatsStage::tearDown(){
	//_waveHolder->tearDown();
	GameState::tearDown();
}

void EatsStage::mouseMoveEvent(int mX,int mY, int relX, int relY, bool Left, bool Right, bool Middle){

	if(isTowerSelected){
		towerSelected->setX(mX - towerSelectedSize/2);
		towerSelected->setY(mY - towerSelectedSize/2);
	}

}

void EatsStage::lButtonDownEvent(int mX, int mY)
{
	Point * p = new Point;
	p->x = mX;
	p->y = mY;

	if( isClickOnHud( *p )  ){
		int resposta = hud->getSelectedTower(*p);
		switch (resposta){
			case 1:
				int posX, posY;
				map->getSquareZeroPoint(mX, mY, &posX, &posY);

				towerSelected = new Catodix(_working_surface, 100 , 100 - TowersIndex::CATODIX_TILE_SIZE);
				towerSelectedSize = TowersIndex::CATODIX_TILE_SIZE;

				objList.push_back(towerSelected );
				if(!towerSelected->setUp()){
					std::cout << "|ERROR| Impossível adicionar a torre" << std::endl;
					return;
				}
				isTowerSelected = true;
				cout << "|INFO| Torre Catodix selecionada para construção " << endl;
				break;
			case 2:
				cout << "|INFO| Torre 2 selecionada para construção " << endl;
				break;
			case 3:
				cout << "|INFO| Torre 3 selecionada para construção " << endl;
				break;
		}
		return;
	}

	if( ! isTowerSelected ){
		return;
	}

	if( goldMeter <= 0 || goldMeter-towerSelected->getPrice() < 0 ){
		return;
	}

	if(map->isBlockAvailableForConstruction(mX - (towerSelectedSize/2),mY + (towerSelectedSize/2), towerSelectedSize, towerSelectedSize, 0) )
	{
		int posX, posY;
		map->setBlockAvailableForConstruction(mX - (towerSelectedSize/2) ,mY + (towerSelectedSize/2), towerSelectedSize,towerSelectedSize, false);
		map->getSquareZeroPoint(mX - towerSelectedSize/2, mY + towerSelectedSize/2 , &posX, &posY);

		Catodix *catodix = new Catodix(_working_surface, posX , posY - TowersIndex::CATODIX_TILE_SIZE);

		objList.push_back(catodix);
		if(!catodix->setUp()){
			std::cout << "|ERROR| Impossível adicionar a torre" << std::endl;
			return;
		}
		catodix->_isActive = true;
		std::cout << "|INFO| Torre adicionada e configurada com sucesso" << std::endl;
		goldMeter -= TowersIndex::CATODIX_PRICE;
	} else {
		std::cout << "|INFO| Bloco Indisponível" << std::endl ;
	}

	delete(p);

}

bool EatsStage::destroyNemlinium(int n)
{
	if(nemliniumList.size() <= 0 )
		return false;
	for(int i = 0; i < n; i++){
		Nemlinium * temp = nemliniumList.at(i);
		disableObject(temp);
		nemliniumList.erase(nemliniumList.begin());
	}
	return true;
}

bool EatsStage::isClickOnHud( Point click){
	return CoordinatesHelper::isOnRect(*hud->getPoint(), hud->seletorDeTorres->getWidth(), hud->seletorDeTorres->getHeight(),click);
}

std::vector<BaseEnemy*> EatsStage::getEnemies()
{
	return _waveHolder->getEnemiesList();
}

void EatsStage::keyDownEvent(SDLKey sym, SDLMod mod, Uint16 unicode)
{
	switch(sym) {
		case SDLK_ESCAPE:
		{
			isTowerSelected = false;
			disableObject(towerSelected);
			break;
		}
		case SDLK_F5:
			GameConteiner::getInstance()->getGameManager()->turnOFF();
			break;
		default:
			break;
	}
}

